package dao.impl;

import dao.IReaderDao;
import domain.Reader;
import util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ReaderDao implements IReaderDao {
    //判断是否为读者登录
    public boolean selectReader(String workno, String pwd) {
        //初始化连接
        Connection connection  = JDBCUtil.getConnection();
        //告诉mysql我要干什么
        String sql = "select * from reader where rnum = '" + workno + "' and rpwd='" + pwd + "'";
        //获取数据库操作句柄
        PreparedStatement pstmt = JDBCUtil.getPstmt(connection, sql);
        //获取结果集
        ResultSet rs = JDBCUtil.getRs(pstmt);
        //使用rs.next（）迭代数据，如果有下一条，则返回真，否则返回假
        try {
            //如果结果集当中有数据则说明登录数据存在，否则不存在
            if(rs.next()){
                return  true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.close(rs,pstmt,connection);
        }
        return false;
    }


    @Override
    //查询读者
    public List selectReaders() {
        //需要到数据库中获取数据
        //初始化连接
        Connection connection = JDBCUtil.getConnection();
        //告诉mysql我要干什么
        String sql = "select * from reader";
        //获取数据库操作句柄
        PreparedStatement pstmt = JDBCUtil.getPstmt(connection, sql);
        //获取结果集
        ResultSet rs = JDBCUtil.getRs(pstmt);

        //初始化一个list容器用于放置Teacher的数据
        List<Reader> readerList = new ArrayList<Reader>();
        //使用rs.next（）迭代数据，如果有下一条，则返回真，否则返回假
        try {
            while (rs.next()) {
                //将所有的数据库数据转化为java中的对象，完成数据结构的转换
                Reader reader = new Reader();
                reader.setRname(rs.getString("rname"));
                reader.setRnum(rs.getString("rnum"));
                reader.setRschool(rs.getString("rschool"));
                reader.setRsex(rs.getString("rsex"));
                reader.setRpwd(rs.getString("rpwd"));
                reader.setBorrowbook(rs.getString("borrowbook"));
                //将数据赋给list容器
                readerList.add(reader);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(rs, pstmt, connection);
        }
        return readerList;
    }

    //添加读者
    public int insertReader(Reader reader) {
        //初始化连接
        Connection connection = JDBCUtil.getConnection();
        //告诉mysql我要干什么
        String sql = "insert into reader (rnum,rname,rsex,rschool,rpwd,borrowbook) values(?,?,?,?,?,?)";

        //获取数据库操作句柄
        PreparedStatement pstmt = JDBCUtil.getPstmt(connection, sql);

        //获取结果集
        try {

            pstmt.setString(1, reader.getRnum());
            pstmt.setString(2, reader.getRname());
            pstmt.setString(3, reader.getRsex());
            pstmt.setString(4, reader.getRschool());
            pstmt.setString(5, reader.getRpwd());
            pstmt.setString(6, reader.getBorrowbook());

        } catch (SQLException e) {
            e.printStackTrace();
        }
        int result = JDBCUtil.modify(pstmt);
        return result;
    }
    //删除读者
    public String deleteReader(String num) {//初始化连接
        Connection connection = JDBCUtil.getConnection();
        //告诉mysql我要干什么
        String sql = "delete from reader where rnum='"+num+"'";

        //获取数据库操作句柄
        PreparedStatement pstmt = JDBCUtil.getPstmt(connection, sql);

        int result = JDBCUtil.modify(pstmt);

        return sql;
    }
    //读者借阅图书，修改图书编号
    public int updateBorrowbook(Reader reader) {
        //初始化连接
        Connection connection = JDBCUtil.getConnection();
        //告诉mysql我要干什么
        String sql = "update  reader set borrowbook = '"+ reader.getBorrowbook()+"'where rnum='"+reader.getRnum()+"'";
        //获取数据库操作句柄
        PreparedStatement pstmt = JDBCUtil.getPstmt(connection, sql);

        //获取结果集

        int result = JDBCUtil.modify(pstmt);
        return result;
    }
    //读者归还图书，修改图书编号
    public int returnBorrowbook(Reader reader) {
        //初始化连接
        Connection connection = JDBCUtil.getConnection();
        //告诉mysql我要干什么
        String sql = "update  reader set borrowbook = '"+ reader.getBorrowbook()+"'where rnum='"+reader.getRnum()+"'";
        //获取数据库操作句柄
        PreparedStatement pstmt = JDBCUtil.getPstmt(connection, sql);

        //获取结果集

        int result = JDBCUtil.modify(pstmt);
        return result;
    }

    }



